package com.xiaoma.tpo.dao;

import java.nio.channels.SelectableChannel;
import java.util.List;

import org.codehaus.jackson.map.deser.FromStringDeserializer;
import org.springframework.jdbc.object.SqlCall;
import org.springframework.stereotype.Repository;

import com.xiaoma.tpo.baseDao.BaseDaoImpl;
import com.xiaoma.tpo.domain.MkSingleAnswer;
import com.xiaoma.tpo.domain.User;
import com.xiaoma.tpo.dto.MkListeningQuestionVo;
import com.xiaoma.tpo.dto.MkReadingQuestionVo;
import com.xiaoma.tpo.dto.MkSingleAnswerVo;
import com.xiaoma.tpo.dto.MockResultDto;
import com.xiaoma.tpo.utils.Pager;
import com.xiaoma.verbal.baseDao.DefaultRowMapper;
@Repository
public class MkSingleAnswerDao extends BaseDaoImpl<MkSingleAnswer>{

	public Pager findByUserIdAndTpoId(User user,
			MockResultDto resultDto) {
		Pager pager = new Pager();
		Integer page = resultDto.getPage();
		Integer rows = resultDto.getRows();
		Integer start = 0;
		if(page == null || page == 0 ){
			start = 0;
		}else{
			start = (page -1)*rows;
		}
		String sqlC = "select count(*)  "
				+     " from mk_reading_question mlq left join mk_single_answer msa "
				+     " on msa.mk_question_id = mlq.id "
				+     " and msa.reading_listening_type = 0 "
				+     " and msa.user_id = "+user.getId()+" "
				+     " where mlq.mk_tpo_id  = "+resultDto.getTpoId()+"";
//		        +     " and msa.mk_tpo_id  = "+resultDto.getTpoId();
		Integer totalCount = jdbcTemplate.queryForInt(sqlC);
		String sql = " select * "
				+    " from mk_reading_question mlq left join mk_single_answer msa "
				+    " on  msa.mk_question_id = mlq.id "
				+    " and   msa.reading_listening_type = 0 "
				+    " and msa.user_id = "+user.getId()+" "
				+    " where mlq.mk_tpo_id =  "+resultDto.getTpoId()+" "
//				+    " and msa.mk_tpo_id =  "+resultDto.getTpoId()+" "
				+    " order by mlq.question_num "
				+    " limit "+start+","+rows;
		
		List<MkReadingQuestionVo> mkReadingQuestionVo = jdbcTemplate.query(sql,new DefaultRowMapper(MkReadingQuestionVo.class, this.getNameHandler()));
		pager.setList(mkReadingQuestionVo);
		pager.setTotalCount(totalCount);
		return pager;
	}

	public Pager findListeningByUserIdAndTpoId(User user,
			MockResultDto resultDto) {
		Pager pager = new Pager();
		Integer page = resultDto.getPage();
		Integer rows = resultDto.getRows();
		Integer start = 0;
		if(page == null || page == 0 ){
			start = 0;
		}else{
			start = (page -1)*rows;
		}
		String sqlC =" select count(*) "
				+    " from mk_listening_question mlq  left join mk_listening ml on mlq.mk_listening_id=ml.id  "
				+    " left join  mk_single_answer msa  on msa.reading_listening_type=1"
				+    " and mlq.id = msa.mk_question_id and msa.user_id ="+user.getId()
				+    " where ml.mk_tpo_id = "+resultDto.getTpoId();
		Integer totalCount = jdbcTemplate.queryForInt(sqlC);
		String sql = " select * "
				+    " from mk_listening_question mlq  left join mk_listening ml on mlq.mk_listening_id=ml.id  "
				+    " left join  mk_single_answer msa  on msa.reading_listening_type=1"
				+    " and mlq.id = msa.mk_question_id and msa.user_id ="+user.getId()
				+    " where ml.mk_tpo_id = "+resultDto.getTpoId()
				+    " order by mlq.seq_num "
				+    " limit "+start+","+rows;
		
		List<MkListeningQuestionVo> mkListeningQuestionVo = jdbcTemplate.query(sql,new DefaultRowMapper(MkListeningQuestionVo.class, this.getNameHandler()));
		pager.setList(mkListeningQuestionVo);
		pager.setTotalCount(totalCount);
		return pager;
	}
    //在插入数据之前先查看一下 mk_result 中是否已经保存了这个结果 避免重复
	public String findByUserAndTpo(MkSingleAnswerVo mkSingleAnswerVo, User user) {
		String type = mkSingleAnswerVo.getTestAnswer().get(0).getReadingListeningType();
		String questionID = mkSingleAnswerVo.getTestAnswer().get(0).getMkQuestionId();
		if("0".equals(type)){
			String sqlExist = "select count(*) "
					+ " from mk_reading_question "
					+ " where  mk_tpo_id="+mkSingleAnswerVo.getTpoId()+""
					+ " and id = "+mkSingleAnswerVo.getTestAnswer().get(0).getMkQuestionId();
			Integer count = jdbcTemplate.queryForInt(sqlExist);
			if(count == 0 || count == null){
				return "nothing";
			}
			String sql = " select count(*)  "
					+    " from mk_single_answer "
					+    " where reading_listening_type = '0'"
					+    " and mk_question_id = "+Integer.parseInt(questionID);
			Integer totalCount = jdbcTemplate.queryForInt(sql);
			if(totalCount != null && totalCount >0){
				return "false";
			}
		}
		if("1".equals(type)){
			String sqlExist = "select count(*) "
					+ " from mk_listening ml, mk_listening_question mlq"
					+ " where  ml.id = mlq.mk_listening_id"
					+ " and ml.mk_tpo_id="+mkSingleAnswerVo.getTpoId()+""
					+ " and mlq.id = "+mkSingleAnswerVo.getTestAnswer().get(0).getMkQuestionId();
			Integer count = jdbcTemplate.queryForInt(sqlExist);
			if(count == 0 || count == null){
				return "nothing";
			}
			String sql = " select count(*)  "
					+    " from mk_single_answer "
					+    " where reading_listening_type = '1'"
					+    " and mk_question_id = "+Integer.parseInt(questionID);
			Integer totalCount = jdbcTemplate.queryForInt(sql);
			if(totalCount != null && totalCount >0){
				return "false";
			}
		}
		return "true";
	}
	
	
}
